package com.hotel.config.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class OrderDelayConfig {
    //声明 3个属性，1个路由 key 1个队列 1个交换机
    //延迟交换机
    public static final String ORDER_DELAY_EXCHANGE_NAME = "order.delay.exchange";
    //延迟队列
    public static final String ORDER_DELAY_QUEUE_NAME = "order.delay.queue";
    //延迟队列路由 key
    public static final String ORDER_DELAY_QUEUE_ROUTING_KEY = "order.delay.queue.routingkey";

    //声明延迟交换机
    @Bean(ORDER_DELAY_EXCHANGE_NAME)
    public CustomExchange delayExchange(){
        Map<String,Object> args = new HashMap<>();
        args.put("x-delayed-type","direct");
        return new CustomExchange(ORDER_DELAY_EXCHANGE_NAME,"x-delayed-message",true,false,args);
    }

    //声明延迟队列
    @Bean(ORDER_DELAY_QUEUE_NAME)
    public Queue delayQueue(){
        return new Queue(ORDER_DELAY_QUEUE_NAME,true);
    }


    //声明延迟队列的绑定关系
    @Bean
    public Binding delayBinding(@Qualifier(ORDER_DELAY_QUEUE_NAME) Queue queue,
                                @Qualifier(ORDER_DELAY_EXCHANGE_NAME) CustomExchange delayExchange){
        return BindingBuilder.bind(queue).to(delayExchange).with(ORDER_DELAY_QUEUE_ROUTING_KEY).noargs();
    }

}
